/** 
 * 找个工作
 * 为能加入某组织
 * 比如 New Tokyo 组织 需要移动到 New Tokyo
 * 有些组织需要在某公司打工
 */

var GNS = {};

/**
 * 这个组织有组件未买:
 * @return true:有
 */
function faction_has_augmentations(faction_name)
{
    let ns = GNS;
    var owned_augmentations = ns.singularity.getOwnedAugmentations();
    var augmentations_from_faction = ns.singularity.getAugmentationsFromFaction(faction_name);
    // 1. 取未购买的组件:
    let list_unpurchased = [];
    for(let i=0;i<augmentations_from_faction.length;++i)
    {
        let augmentation_name = augmentations_from_faction[i];
        if(-1!= owned_augmentations.indexOf( augmentation_name))continue;
        list_unpurchased.push(augmentation_name);
    }// END for

    return 0!=list_unpurchased.length;

    
    return false;
}

let list_of_Job_field = [
    "Agent",
    "Business",
    "Business Consultant",
    "Employee",
    "IT",
    "Network Engineer",
    "Part-time Employee",
    "Part-time Waiter",
    "Security",
    "Security Engineer",
    "Software",
    "Software Consultant",
    "Waiter"
];

export async function main(ns) {
    GNS = ns;
    // 最保险的方案: 在没有工作时再去工作
    let bool_busy = ns.singularity.isBusy();
    if(!bool_busy)
    {
        //let current_work = ns.singularity.getCurrentWork();

        // New Tokyo:
        // 1. 组织有组件未买:
        if(faction_has_augmentations('New Tokyo'))
        {
            ns.tprint("该加入",'New Tokyo');
            // 移动到New Tokyo
            ns.singularity.travelToCity('New Tokyo');
            //找工作:
            let bool_find_job = false;
            for(let index_of_job in list_of_Job_field)
            {
                let job_name = list_of_Job_field[index_of_job];
                let found_name = ns.singularity.applyToCompany('Global Pharmaceuticals', job_name)
                if(found_name)
                {
                    bool_find_job = true;
                    ns.tprint("找到了工作:",found_name);
                }
                
            }
            if(bool_find_job)
            {
                ns.singularity.workForCompany('Global Pharmaceuticals', true);
            }
        }
    }
}